package com.springboot.uav.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.uav.entity.User;
import com.springboot.uav.entity.UserMsg;
import com.springboot.uav.vo.MyPageUserVo;
import org.apache.ibatis.annotations.*;

/**
 * Created by IntelliJ IDEA.
 * &#064;Author : Zys
 * &#064;create 2024/10/31 14:01
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    //扣余额
    @Update("update user set balance=balance-#{reward} where user_id=#{userId} ")
    void deductionBalance(double reward,Integer userId);

    @Select("select balance from user where user_id=#{customerId}")
    double getBalanceById(Integer customerId);

    @ResultMap("userMsgMap")
    @Select("SELECT u.user_id, u.nickname, u.email, u.phone_number, u.user_grade, u.balance, " +
            "u.sex,u.avatar,"+
            "a.add_id,a.address, a.address_type " +
            "FROM user u " +
            "LEFT JOIN address a ON u.user_id = a.user_id " +
            "WHERE u.user_id = #{userId}")
    UserMsg getUserMsgById(Integer userId);

    // 更新昵称
    @Update("UPDATE user SET nickname = #{newNickname} WHERE user_Id = #{userId}")
    int updateNicknameById(@Param("userId") Integer userId, @Param("newNickname") String newNickname);

    // 更新密码
    @Update("UPDATE user SET password = #{newPassword} WHERE user_Id = #{userId}")
    int updatePasswordById(@Param("userId") Integer userId, @Param("newPassword") String newPassword);

    //查询用户昵称、性别、电话号码
    @Select("SELECT u.nickname, u.sex, u.phone_number FROM user u WHERE u.user_id = #{userId}")
    MyPageUserVo getUserInfo(@Param("userId") Integer userId);
}
